package com.example.demo.service.impl;

import com.example.demo.entity.Furniture;
import com.example.demo.mapper.furnituremapper;
import com.example.demo.service.FurnitureService;
import com.example.demo.util.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class FurnitureServiceImpl implements FurnitureService {
    @Autowired
    furnituremapper furnituremapper;
    //对家具进行分页查询
    public List<Furniture> pageFurniture(int page, int size, String query){
        List<Furniture> page1 = furnituremapper.page(page, size, query);
        log.info(123+page1.toString());
        if(page1.size() ==0){
            throw new ServiceException("暂时没有家具信息");
        }
        // //故要通过stream便利在每个字段添加值
        //        flavors = flavors.stream().map((item) -> {
        //            item.setDishId(dishDto.getId());
        //            return item;
        //        }).collect(Collectors.toList());
        List<Furniture> collect = page1.stream().map((item) -> {
            //由于前端显示注销为布尔类型，而数据库没有，故多加了个字段，转化为布尔类型
            item.setStatu((item.getStatus() == 0) ? true : false);
            return item;
        }).collect(Collectors.toList());
        return collect;
    }
    //对家具进行查询
    public List<Furniture> searchFurniture(String query){
        List<Furniture> search = furnituremapper.search(query);
        return search;
    }
    //对家具进行添加
    @Override
    public int add(Furniture furniture) {
        int i=furnituremapper.add(furniture);
        if(i<=0){
            throw new ServiceException("数据添加失败");
        }
        return i;
    }

    @Override
    public void changeFurniture(Furniture furniture) {
        int i=furnituremapper.updateFurniture(furniture);
        if(i<=0){
            throw new ServiceException("修改失败");
        }
    }
}
